<?php
/**
 * Created by PhpStorm.
 * User: qzy008
 * Date: 2018/9/8
 * Time: 16:11
 */

namespace app\api\service;

use app\lib\enum\ScopeEnum;
use app\lib\exception\TokenException;

class AdminToken extends Token
{
    public function prepareCacheValue($data) {
        $cacheValue['adminID'] = $data['id'];
        $cacheValue['scope'] = ScopeEnum::Super;

        return $cacheValue;
    }

    public function saveToCache($data) {
        $token = self::generateToken();
        $cacheValue = $this->prepareCacheValue($data);
        $expire_in = config('setting.token_expire_in');

        $result = cache($token, $cacheValue, $expire_in);
        if (!$result) {
            throw new TokenException([
                'msg' => '服务器缓存异常',
                'error_code' => 10005
            ]);
        }
        return $token;
    }

    // 颁发token令牌
    public function grantToken($data) {
        return $this->saveToCache($data);
    }
}